/**
 * Copyright (c) 2007-2008 Nearchos Paspallis
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */

/**
 * AspectSense
 *
 * Middleware for annotation-based, context-aware and adaptive services for
 * mobile and ubiquitous computing
 *
 * This project is an open-source effort, initiated by Nearchos Paspallis as
 * part of his PhD work at the University of Cyprus (http://www.ucy.ac.cy).
 *
 * More information about the project is available at "http://aspectsense.org".
 */

package org.aspectsense.gui.util;

import org.aspectsense.framework.Main;

import javax.swing.*;
import javax.swing.plaf.basic.BasicSplitPaneUI;
import java.awt.*;
import java.awt.event.ComponentListener;
import java.awt.event.ComponentEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

/**
 * @author Nearchos Paspallis [nearchos@cs.ucy.ac.cy]
 *         Date: Jan 14, 2008
 *         Time: 3:06:38 PM
 */
public class AboutFrame
{
    public static final int PREFERRED_WIDTH = 350;
    public static final int PREFERRED_HEIGHT = 200;

    public static final String TITLE = "AspectSense Desktop";
    public static final String ABOUT_MESSAGE = "<html>"
            + "<b><h3>&nbsp;AspectSense " + Main.COPYRIGHT  + "</b></h3>"
            + "<i>&nbsp;Enabling context-aware, adaptive applications</i><br>"
            + "<br>"
            + "&nbsp;Version " + Main.VERSION + "<br>"
            + "<font color=#000080>&nbsp;" + Main.WEBSITE_URL + "</font>"
            + "</html>";

    private static JFrame aboutFrame = null;

    static public void show()
    {
        synchronized(AboutFrame.class)
        {
            if(aboutFrame == null)
            {
                aboutFrame = new JFrame(TITLE) {
                    public Insets getInsets() {
                        return new Insets(4, 4, 4, 4);
                    }
                };
                aboutFrame.setUndecorated(true);
                aboutFrame.addMouseListener(new MouseAdapter() {
                    public void mouseClicked(MouseEvent e) {
                        hide();
                    }
                });

                final JLabel aboutLabel = new JLabel(ABOUT_MESSAGE) {
                    public Insets getInsets() {
                        return new Insets(8, 8, 8, 8);
                    }
                };

                final JLabel javaLabel = new JLabel("Java "
                        + System.getProperty("java.version")
                        + " by " + System.getProperty("java.vendor"));
                final JLabel osLabel = new JLabel(System.getProperty("os.name")
                        + " version " + System.getProperty("os.version")
                        + " on " + System.getProperty("os.arch"));

                final JPanel systemPanel = new JPanel(new BorderLayout()) {
                    public Insets getInsets() { return new Insets(4, 4, 4, 4); }
                };

                systemPanel.add(javaLabel, BorderLayout.NORTH);
                systemPanel.add(osLabel, BorderLayout.SOUTH);
                systemPanel.setBackground(new Color(247, 247, 247));
                systemPanel.setOpaque(true);
                systemPanel.setBorder(BorderFactory.createLoweredBevelBorder());

                final JPanel mainPanel = new JPanel(new BorderLayout()) {
                    public Insets getInsets() { return new Insets(4, 4, 4, 4); }
                };

                mainPanel.add(aboutLabel, BorderLayout.NORTH);
                mainPanel.add(systemPanel, BorderLayout.SOUTH);

                mainPanel.setBorder(BorderFactory.createEtchedBorder());

                aboutFrame.add(mainPanel);
            }

            final Dimension screeDimension = Toolkit.getDefaultToolkit().getScreenSize();
            aboutFrame.setSize(PREFERRED_WIDTH, PREFERRED_HEIGHT);
            aboutFrame.setLocation(
                    (int) (screeDimension.getWidth() - aboutFrame.getWidth()) / 2,
                    (int) (screeDimension.getHeight() - aboutFrame.getHeight()) / 2);
            aboutFrame.setVisible(true);
        }
    }

    static public void hide()
    {
        aboutFrame.setVisible(false);
    }
}
